﻿using Microsoft.Win32;
using MySql.Data.MySqlClient;
using System;
using System.Data;
using System.Windows;

namespace HolterDatabase.Model
{
    public abstract class DatabaseSet
    {
        public abstract MySqlDataAdapter GetAdapter(MySqlConnection connection);

        public bool UpdateTable() 
        {
            try
            {
                using (MySqlConnection conn = new MySqlConnection(connectionString))
                {
                    conn.Open();
                    var adapter = GetAdapter(conn);
                    adapter.Update(DataTable);
                    conn.Close();
                    return true;
                }
                
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
                return false;
            }
        }

        public void RefreshTable()
        {
            try
            {
                using (MySqlConnection conn = new MySqlConnection(connectionString))
                {
                    conn.Open();
                    var adapter = GetAdapter(conn);
                    adapter.Fill(DataTable);
                    conn.Close();
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }

        public virtual DataTable GetTable(String sortBy = "")
        {
            using (MySqlConnection conn = new MySqlConnection(connectionString))
            {
                conn.Open();
                var adapter = GetAdapter(conn);
                DataTable = new DataTable();
                RefreshTable();
                DataTable.DefaultView.Sort = sortBy;
                conn.Close();
                return DataTable;
            }
        }

        public void Export()
        {
            SaveFileDialog saveDialog = new SaveFileDialog();
            saveDialog.Filter = "SQL File|*.sql";
            saveDialog.FileName = "backup";
            saveDialog.Title = "Save As";
            if ((bool)(saveDialog.ShowDialog()))
            {
                string file = saveDialog.FileName;
                using (MySqlConnection conn = new MySqlConnection(connectionString))
                {
                    using (MySqlCommand cmd = new MySqlCommand())
                    {
                        using (MySqlBackup mb = new MySqlBackup(cmd))
                        {
                            cmd.Connection = conn;
                            conn.Open();
                            mb.ExportToFile(file);
                            conn.Close();
                        }
                    }
                }
            }
        }

        public void Import()
        {
            OpenFileDialog openDialog = new OpenFileDialog();
            openDialog.Filter = "SQL File|*.sql";
            openDialog.FileName = "backup";
            openDialog.Title = "Open";
            if ((bool)(openDialog.ShowDialog()))
            {
                string file = openDialog.FileName;
                using (MySqlConnection conn = new MySqlConnection(connectionString))
                {
                    using (MySqlCommand cmd = new MySqlCommand())
                    {
                        using (MySqlBackup mb = new MySqlBackup(cmd))
                        {
                            cmd.Connection = conn;
                            conn.Open();
                            mb.ImportFromFile(file);
                            conn.Close();
                        }
                    }
                }
            }
        }

        public DataTable DataTable
        {
            get
            {
                return dataTable;
            }
            protected set
            {
                dataTable = value;
            }
        }

        public static string connectionString = "server=" + Properties.Settings.Default.Server + ";uid=" + Properties.Settings.Default.User + ";database=" + Properties.Settings.Default.DatabaseName + ";";
        private DataTable dataTable;
    }
}
